﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Generator.Selections.DataBase;

namespace Generator.Selections.Windows
{
    public partial class Logon : Form
    {
        public Logon()
        {
            InitializeComponent();
        }

        private void logonButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(databaseTextBox.Text) ||
                string.IsNullOrEmpty(userNameTextBox.Text) ||
                string.IsNullOrEmpty(passwordTextBox.Text) 
                )
            {
                DialogResult = DialogResult.No;
                Close();
            }
            else
            {
                var ch = ConnectionHandler.Instance;

                ch.SetConnectionString(userNameTextBox.Text, passwordTextBox.Text, databaseTextBox.Text);
                
                Cursor = Cursors.WaitCursor;
                try
                {
                    var res = ch.CheckConnection();
                    if (!string.IsNullOrEmpty(res))
                    {
                        Cursor = Cursors.Default;
                        MessageBox.Show(res, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
                finally
                {
                    Cursor = Cursors.Default;
                }
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            DialogResult= DialogResult.Cancel;
            Close();
        }
    }
}
